/**
 * // Definition for a Node.
 * function Node(val, children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number[]}
 */
export var preorder = function (root) {
  const ans = []
  dfs(root, ans)
  return ans
}

function dfs(root, ans) {
  if (!root) {
    return
  }
  ans.push(root.val)
  if (root.children) {
    for (let i = 0; i < root.children.length; i++) {
      dfs(root.children[i], ans)
    }
  }
}
